/*
 * @Author: szx
 * @Date: 2022-04-19 16:53:31
 * @LastEditTime: 2022-04-19 17:03:42
 * @Description:
 * @FilePath: \leetcode\300-399\386\386.js
 */
/**
 * @param {number} n
 * @return {number[]}
 */
var lexicalOrder = function (n) {
    const res = [];
    const dfs = (start, end) => {
        for (let i = start; i <= n && i <= end; ++i) {
            res.push(i);
            dfs(10 * i, 10 * (i + 1) - 1);
        }
    };
    dfs(1, 9);
    return res;
};

var lexicalOrder = function (n) {
    const res = [];
    let j = 1;
    for (let i = 0; i < n; i++) {
        res.push(j);
        if (j * 10 <= n) {
            j = j * 10;
        } else {
            while (j % 10 == 9 || j + 1 > n) j /= 10;
            j++;
        }
    }
    return res;
};
console.log(lexicalOrder(2));
